package com.Smart.smartscals.CustomView;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.Smart.smartscals.Activity.R;
import com.Smart.smartscals.Entity.EntityConversionTime;
import com.Smart.smartscals.Utils.Tools;

public class CustomViewChoiceTime extends LinearLayout implements
		OnClickListener {
	private Context mContext;
	private View mView;
	private ImageView imgBefore, imgNext;
	private TextView tvDate;

	private int dateChoiceType;
	private EntityConversionTime conversionTimeFirst, conversionTimeLast;

	public CustomViewChoiceTime(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		mView = ((LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
				R.layout.customview_choicetime_1, this);
		initView();
		initParam();

	}

	private void initParam() {
		dateChoiceType = 0;
	}

	/**
	 * 
	 * @param dateChoiceType
	 *            1:周 2：月 3：季 4：年
	 */
	public void startMytration(int dateChoiceType) {
		this.dateChoiceType = dateChoiceType;
		initFirstDateStr();
		setListener();

	}

	private void initFirstDateStr() {

		setTvDateData(Tools.getCurrentTimeEntity());

	}

	private void setTvDateData(EntityConversionTime conversionTime) {

		if (dateChoiceType == 1) {

			// 获取前后的实体类；
			conversionTimeFirst = Tools.getWeekFirstDay(conversionTime);
			conversionTimeLast = Tools.getWeekLastDay(conversionTime);
			Tools.setLog("----L>setTvDateData:" + conversionTime.getDayNUM()
					+ "all:" + conversionTime.getMonthAllday());
			tvDate.setText(conversionTimeFirst.getMonthNUM()
					+ mContext.getString(R.string.month)
					+ conversionTimeFirst.getDayNUM()
					+ mContext.getString(R.string.day) + "-"
					+ conversionTimeLast.getMonthNUM()
					+ mContext.getString(R.string.month)
					+ conversionTimeLast.getDayNUM()
					+ mContext.getString(R.string.day));
		}
	}

	private void initView() {
		imgBefore = (ImageView) mView
				.findViewById(R.id.customview_choiceview1_img_before);
		imgNext = (ImageView) mView
				.findViewById(R.id.customview_choiceview1_img_next);
		tvDate = (TextView) mView
				.findViewById(R.id.customview_choiceview1_tv_date);

	}

	private void setListener() {
		imgBefore.setOnClickListener(this);
		imgNext.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		if (dateChoiceType == 0)
			return;
		switch (v.getId()) {
		case R.id.customview_choiceview1_img_before:
			btnBeforeDoJob();
			break;
		case R.id.customview_choiceview1_img_next:
			btnNextDoJob();
			break;

		default:
			break;
		}

	}

	private void btnNextDoJob() {
		if (conversionTimeLast.getDayNUM() < conversionTimeLast
				.getMonthAllday()) {// 说明在同一个月
			conversionTimeLast.setDayNUM(conversionTimeLast.getDayNUM() + 1);
			conversionTimeLast.setWeekNUM(1);

			Tools.setLog("----L>btnNextJob:" + conversionTimeLast.getDayNUM()
					+ "all:" + conversionTimeLast.getMonthAllday());
			setTvDateData(conversionTimeLast);
		} else {

			EntityConversionTime mConversionTime = Tools
					.getNextMonthConversionEntitiy(conversionTimeLast);
			conversionTimeLast.setWeekNUM(1);
			setTvDateData(mConversionTime);

		}
	}

	private void btnBeforeDoJob() {

		if (conversionTimeFirst.getDayNUM() - 1 > 0) {// 同一个月

			conversionTimeFirst.setDayNUM(conversionTimeFirst.getDayNUM() - 1);
			conversionTimeFirst.setWeekNUM(7);
			Tools.setLog("----L>btnBeforeDoJob:"
					+ conversionTimeFirst.getDayNUM());
			setTvDateData(conversionTimeFirst);

		} else {

			EntityConversionTime mConversionTime = Tools
					.getLastMonthConversionEntitiy(conversionTimeFirst);
			mConversionTime.setWeekNUM(7);
			setTvDateData(mConversionTime);

		}

	}

}
